package com.yanfan.usersmart.user.tools;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/*中国移动摄像头监控工具类*/
@Component  // 添加此注解
@Slf4j
public class UserSmartTools {

    private static final String ACC_ID = "";
    private static final String ACC_KEY = "";
    private static final String URL = "";

    public String getSign(Long time, int random) throws Exception {
        // 拼接字符串
        String input = ACC_KEY + time + random;
        // 计算 MD5
        return generateSignature(input);
    }

    /**
     * 查询所有设备分类（字典）
     * @param time
     * @param random
     * @return
     * @throws Exception
     */
    public String deviceClassify(Long time, int random) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/deviceClassify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }



    /**
     * 查询模拟量名称、单位列表
     * @param time
     * @param random
     * @return
     * @throws Exception
     */
    public String analogQuantity(Long time, int random) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/analogQuantity")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 查询所有系统类型（字典）
     * @param time
     * @param random
     * @return
     * @throws Exception
     */
    public String sysType(Long time, int random) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/sysType")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }


    /**
     * 查询所有上报数据类型（字典）
     * @param time
     * @param random
     * @return
     * @throws Exception
     */
    public String dataType(Long time, int random) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/dataType")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 查询所有设备信息状态（字典）
     * @param time
     * @param random
     * @param id
     * @param data_type_id
     * @return
     * @throws Exception
     */
    public String deviceStatus(Long time, int random,String id,String data_type_id) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/deviceStatus")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("id",id)
                .header("data_type_id",data_type_id)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 查询所有设备信息状态（字典）
     * @param time
     * @param random
     * @return
     * @throws Exception
     */
    public String deviceStatus(Long time, int random) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/deviceStatus")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 根据设备分类 id 查询下行指令（字典）
     * @param time
     * @param random
     * @param id
     * @return
     * @throws Exception
     */
    public String queryOperation(Long time, int random,String id) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/queryOperation")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("id",id)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }


    /**
     * 查询设备类型列表(字典)
     * @param time
     * @param random
     * @param id
     * @param device_classify_id
     * @param sys_type_id
     * @return
     * @throws Exception
     */
    public String deviceType(Long time, int random,String id,String device_classify_id,String sys_type_id) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/deviceType")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("id",id)
                .header("device_classify_id",device_classify_id)
                .header("sys_type_id",sys_type_id)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 设备下行控制
     * @param time
     * @param random
     * @param device_id
     * @param code
     * @param operation
     * @return
     * @throws Exception
     */
    public String downlinkOperation(Long time, int random,String device_id,String code,String operation) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/downlinkOperation")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_id",device_id)
                .header("code",code)
                .header("operation",operation)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }



    /**
     * 采集模拟量
     * @param time
     * @param random
     * @param device_id
     * @param minute
     * @return
     * @throws Exception
     */
    public String startAnalogQuantity(Long time, int random,String device_id,int minute) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/startAnalogQuantity")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_id",device_id)
                .header("minute", String.valueOf(minute))
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 设备批量导入
     * @param time
     * @param random
     * @param params
     * @return
     * @throws Exception
     */
    public String importDevice(Long time, int random,Map<String, Object> params) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        //请求参数
        JSONObject requestBody = new JSONObject();
        requestBody.putAll(params);
        String jsonBody = requestBody.toJSONString();
        String result = HttpRequest.post(URL+"/external/importDevice")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .body(jsonBody)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }


    /**
     * 添加网络设备
     * @param time
     * @param random
     * @param sys_mac
     * @return
     * @throws Exception
     */
    public String append(Long time, int random,String sys_mac) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/append")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("sys_mac",sys_mac)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 添加网络设备
     * @param time
     * @param random
     * @param sys_mac
     * @param device_name
     * @return
     * @throws Exception
     */
    public String append(Long time, int random,String sys_mac,String device_name) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/append")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("sys_mac",sys_mac)
                .header("device_name",device_name)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 添加网络设备
     * @param time
     * @param random
     * @param sys_mac
     * @param device_name
     * @param device_position
     * @return
     * @throws Exception
     */
    public String append(Long time, int random,String sys_mac,String device_name,String device_position) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/append")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("sys_mac",sys_mac)
                .header("device_name",device_name)
                .header("device_position",device_position)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 通过 device_id 修改网络设备的设备名称、安装位置
     * @param time
     * @param random
     * @param device_id
     * @param device_name
     * @param device_position
     * @return
     * @throws Exception
     */
    public String modify(Long time, int random,String device_id,String device_name,String device_position) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/modify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_id",device_id)
                .header("device_name",device_name)
                .header("device_position",device_position)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }


    /**
     * 通过 device_id 修改网络设备的设备名称、安装位置
     * @param time
     * @param random
     * @param device_id
     * @param device_name
     * @return
     * @throws Exception
     */
    public String modify(Long time, int random,String device_id,String device_name) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/modify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_id",device_id)
                .header("device_name",device_name)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 通过 device_id 修改网络设备的设备名称、安装位置
     * @param time
     * @param random
     * @param device_id
     * @return
     * @throws Exception
     */
    public String modify(Long time, int random,String device_id) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/modify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_id",device_id)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }


    /**
     * 修改网络设备唯一 ID 码
     * @param time
     * @param random
     * @param before_sys_mac
     * @param after_sys_mac
     * @return
     * @throws Exception
     */
    public String modifySys_mac(Long time, int random,String before_sys_mac,String after_sys_mac) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/modifySys_mac")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("before_sys_mac",before_sys_mac)
                .header("after_sys_mac",after_sys_mac)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }


    /**
     * 查询某一网络设备
     * @param time
     * @param random
     * @param device_id
     * @return
     * @throws Exception
     */
    public String get(Long time, int random,String device_id) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/get")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_id",device_id)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 通过设备分类查询该分类所有设备(查询网络设备)
     * @param time
     * @param random
     * @param device_classify_id
     * @param page
     * @return
     * @throws Exception
     */
    public String byDeviceClassify(Long time, int random,String device_classify_id,String page,String limit) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/byDeviceClassify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_classify_id",device_classify_id)
                .header("page",page)
                .header("limit",limit)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 通过设备分类查询该分类所有设备(查询网络设备)
     * @param time
     * @param random
     * @param device_classify_id
     * @param page
     * @return
     * @throws Exception
     */
    public String byDeviceClassify(Long time, int random,String device_classify_id,String page ) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/byDeviceClassify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_classify_id",device_classify_id)
                .header("page",page)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 通过设备分类查询该分类所有设备(查询网络设备)
     * @param time
     * @param random
     * @param device_classify_id
     * @return
     * @throws Exception
     */
    public String byDeviceClassify(Long time, int random,String device_classify_id) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/byDeviceClassify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_classify_id",device_classify_id)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 删除网络设备
     * @param time
     * @param random
     * @param device_id
     * @param isRemove 下面有级联设备时，传 true 会一并删除；传 false 时，无级联设备直接删除，
     * 有级联设备会返回提示语
     * @return
     * @throws Exception
     */
    public String remove(Long time, int random,String device_id,boolean isRemove) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/remove")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("device_id",device_id)
                .header("isRemove", String.valueOf(isRemove))
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 添加系统设备
     * @param time
     * @param random
     * @param net_id
     * @param device_type_id
     * @param main_code
     * @param sub_code
     * @param device_name
     * @param device_position
     * @return
     * @throws Exception
     */
    public String appendControl(Long time, int random,String net_id,String device_type_id,String main_code,String sub_code,String device_name,String device_position) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/appendControl")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("net_id",net_id)
                .header("device_type_id", device_type_id)
                .header("main_code", main_code)
                .header("sub_code", sub_code)
                .header("device_name", device_name)
                .header("device_position", device_position)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 添加部件设备
     * @param time
     * @param random
     * @param net_id
     * @param control_id
     * @param device_type_id
     * @param loop_code
     * @param add_code
     * @param pass_code
     * @param device_name
     * @param device_position
     * @return
     * @throws Exception
     */
    public String appendPart(Long time, int random,String net_id,String control_id, String device_type_id,String loop_code,String add_code,String pass_code,String device_name,String device_position) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/device/appendPart")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("net_id",net_id)
                .header("control_id",control_id)
                .header("device_type_id", device_type_id)
                .header("loop_code", loop_code)
                .header("add_code", add_code)
                .header("pass_code", pass_code)
                .header("device_name", device_name)
                .header("device_position", device_position)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }


    /**
     * 查询推送地址
     * @param time
     * @param random
     * @return
     * @throws Exception
     */
    public String pushUrl(Long time, int random) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/pushUrl")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 修改推送地址
     * @param time
     * @param random
     * @param url
     * @return
     * @throws Exception
     */
    public String pushUrlModify(Long time, int random,String url) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/pushUrl/modify")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .header("url", url)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /**
     * 查询推送失败数据
     * @param time
     * @param random
     * @return
     * @throws Exception
     */
    public String pushFalse(Long time, int random) throws Exception {
        String input = ACC_KEY + time + random;
        String signature = generateSignature(input);
        String result = HttpRequest.post(URL+"/external/pushFalse")
                .header("Content-Type", "application/json")
                .header("acc_id", ACC_ID)
                .header("time", String.valueOf(time))
                .header("random", String.valueOf(random))
                .header("sign", signature)
                .timeout(10000)  // 超时时间10秒
                .execute()
                .body();
        return result;
    }

    /*
     *签名生成工具
     */
    private static String generateSignature( String input) throws Exception {
        try {
            // 获取 MD5 实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算哈希值
            byte[] hashBytes = md.digest(input.getBytes());
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }



}
